/*!
 * @Author: CPS
 * @email: 373704015@qq.com
 * @Date: 2020-08-20 14:27:04
 * @Last Modified by: CPS
 * @Last Modified time: 2020-09-22 15:00:17
 * @Projectname Electron-fileWatcher
 * @file_path Z:\CPS\IDE\SublimeText\3176x32前端\Data\Packages\testt\TesttBeautify\stylus-beautify.js
 * @Filename stylus-beautify.js
 */
'use strict'

// 导入处理模块
const stylusSupremacy = require('stylus-supremacy'),
  defaultOptions = require('./stylus.defaultOptions.js'),
  fs = require('fs')

// 判断环境
if (process.mainModule.filename === __filename) {
  const target = './test/test.stylus'

  //用户配置
  const userOptions = {
    insertColons: false, //是否使用冒号
    insertSemicolons: false, //是否使用分号结尾
    insertBraces: true, //是否使用大括号
    "sortProperties": ["position", "", "width", "height"],
  }

  // 导入stylus文件 (必须时字符串)
  const stylusContent = fs.readFileSync(target).toString()

  // 更新配置
  if (userOptions) Object.assign(defaultOptions, userOptions)

  // 格式化 test.stylus
  console.log(stylusSupremacy.format(stylusContent, defaultOptions))

  //退出
  process.exit()
}

// 执行格式化
module.exports = (stylusContent, formattingOptions) => {
  if (!stylusContent) return null
  let res

  if (formattingOptions) Object.assign(defaultOptions, formattingOptions)
  try {
    res = stylusSupremacy.format(
      stylusContent, defaultOptions
    )

    // res = JSON.stringify(defaultOptions).toString()
  } catch (e) {
    // statements
    console.log(e)
    res = false
  }
  return res
}
